package cn.com.zcode.dic.jstl;

import java.io.IOException;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class DictionaryTag extends SimpleTagSupport {
	public String code;
	public boolean isType;
	public String init;

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter writer = this.getJspContext().getOut();
		if (isType) {
			Map<String, String> dicts = DictionaryUtil.type(code);
			StringBuffer sb = new StringBuffer();
			for (String code : dicts.keySet()) {
				String check = code.equals(init) ? " selected='selected'" : "";
				sb.append("<option value='" + code + "'" + check + ">");
				sb.append(dicts.get(code));
				sb.append("</option>");
				sb.append("\n");
			}
			writer.append(sb);

		} else {
			String title = DictionaryUtil.title(code);
			if (title != null) {
				writer.append(title);
			}

		}
		super.doTag();
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public boolean getIsType() {
		return isType;
	}

	public void setIsType(boolean isType) {
		this.isType = isType;
	}

	public String getInit() {
		return init;
	}

	public void setInit(String init) {
		this.init = init;
	}

}
